package spe.model.spem;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

public class MethodLibraryImpl extends SPEMElementImpl implements MethodLibrary
{	
	
	
	protected List<MethodPlugin> methodPlugins = null;
	protected List<MethodConfiguration> predefinedConfigs = null;
	
	public void addMethodPlugin(MethodPlugin mp)
	{
		if(methodPlugins == null)
			methodPlugins = new ArrayList<MethodPlugin>();
		
		//mp.setParent(this);
		methodPlugins.add(mp);
		//System.out.println(String.format("[MethodLibraryImpl]source = %s; Child MethodPlugin: %s", this, mp));
		firePropertyChange("Element", null, mp);
	}

	@Override
	public List<MethodPlugin> getOwnedMethodPlugins() 
	{
		// TODO Auto-generated method stub
		return methodPlugins;
	}

	@Override
	public void setOwnedMethodPlugins(List<MethodPlugin> methodPluginList) 
	{
		List<MethodPlugin> oldValue = methodPlugins;
		for(MethodPlugin mp: methodPluginList)
			mp.setParent(this);
		
		this.methodPlugins = methodPluginList;
		firePropertyChange("OwnedMethodPlugins", oldValue, methodPluginList);
	}

	@Override
	public void removeMethodPlugin(MethodPlugin mp)
	{
		mp.setParent(null);
		methodPlugins.remove(mp);
		firePropertyChange("MethodPlugin", mp, null);
	}

	@Override
	public List<MethodConfiguration> getOwnedConfigurations() 
	{
		return this.predefinedConfigs;
	}

	@Override
	public void setOwnedConfigurations(List<MethodConfiguration> configList) 
	{
		List<MethodConfiguration> oldValue = this.predefinedConfigs;
		for(MethodConfiguration mc: configList)
			mc.setParent(this);
		predefinedConfigs = configList;
		firePropertyChange("PredefinedConfigs", oldValue, configList);
	}

	@Override
	public void addMethodConfiguration(MethodConfiguration mc)
	{
		if(predefinedConfigs == null)
			predefinedConfigs = new ArrayList<MethodConfiguration>();
		
		//mc.setParent(this);
		predefinedConfigs.add(mc);
		
		firePropertyChange("Element", null, mc);
	}

	@Override
	public void removeMethodConfiguration(MethodConfiguration mc)
	{
		mc.setParent(null);
		predefinedConfigs.remove(mc);
		firePropertyChange("MethodConfiguration", mc, null);
	}

	@Override
	public List<SPEMElement> getChildElements() 
	{
		List<SPEMElement> lst = new ArrayList<SPEMElement>();
		
		if(methodPlugins != null)
			lst.addAll(methodPlugins);
		
		if(predefinedConfigs != null)
			lst.addAll(predefinedConfigs);
		
		return lst;
	}
	
	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) 
	{
		propertyChangeSupport.addPropertyChangeListener(listener);
		
		List<SPEMElement> lst = getChildElements();
		for(SPEMElement e: lst)
			e.addPropertyChangeListener(listener);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addChildElement(SPEMElement child) 
	{
		if(MethodConfiguration.class.isInstance(child))
			addMethodConfiguration((MethodConfiguration)child);
		
		if(MethodPlugin.class.isInstance(child))
			addMethodPlugin((MethodPlugin)child);
	}

	@Override
	public void removeChildElement(SPEMElement child) 
	{
		if(MethodConfiguration.class.isInstance(child))
			removeMethodConfiguration((MethodConfiguration)child);
		
		if(MethodPlugin.class.isInstance(child))
			removeMethodPlugin((MethodPlugin)child);
		
	}
}
